package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.LoginActivity;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.TabOneScheduleAdapter;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.enterprise.act.EntAuthActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.HomeEnterActivity;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.BindEntEntity;
import com.csweimei.rentmanagementtreasure.http.entity.CompanyBean;
import com.csweimei.rentmanagementtreasure.http.entity.TabOne2Bean;
import com.csweimei.rentmanagementtreasure.http.entity.TabOneBean;
import com.csweimei.rentmanagementtreasure.http.entity.TabOneScheduleBean;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.FollowRecordActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.CustomerActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.house.HouseInfoActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.ManageLeaseActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.house.NoticeHouseActivity;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.weight.myProgressView.SemicircleProgressView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FOneVM extends BaseViewModel<Repository> {

    public ObservableField<String> szNum = new ObservableField<>("0");
    public ObservableField<String> htNum = new ObservableField<>("0");

    public ObservableField<String> selType = new ObservableField<>("待收租");

    public FOneVM(Application mApplication, Repository mRepository) {
        super(mApplication, mRepository);
    }

    public boolean isEntAuth = false;

    @Override
    public void onResume() {
        super.onResume();
        getHouseMsg(selType.get());
        getFollowData();
        getSchedule();
        if (model.isLogin()){
            getEntAuth();
        }
    }
    public ObservableField<CompanyBean.DataDTO> entData = new ObservableField<>();

    private void getEntAuth() {
        model.getcompany_info(AppConfig.getCurMap(model))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<CompanyBean>() {
                    @Override
                    public void onNext(CompanyBean o) {
                        if (o.issucc){
                            if (o.msg.equals("未提交申请企业认证")){
                                isEntAuth = false;
                            }else {
                                isEntAuth = true;
                                entData.set(o.getData());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public BindingCommand entClick = new BindingCommand(() -> {
        if (model.isLogin()){
            if (isEntAuth){
                Bundle bundle = new Bundle();
                bundle.putSerializable(AppConfig.BUNDLE_KEY,entData.get());
                startActivity(HomeEnterActivity.class,bundle);
            }else {
                startActivity(EntAuthActivity.class);
            }
        }else {
            startActivity(LoginActivity.class);
        }
    });


    public BindingCommand houseInfoClick = new BindingCommand(() -> {
        if (model.isLogin()){
            Bundle bundle = new Bundle();
            bundle.putString("flag","show");
            startActivity(HouseInfoActivity.class,bundle);
        }else {
            startActivity(LoginActivity.class);
        }

    });

    public BindingCommand zyClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                startActivity(ManageLeaseActivity.class);
            }else {
                startActivity(LoginActivity.class);
            }
        }
    });

    public BindingCommand customClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                startActivity(CustomerActivity.class);
            }else {
                startActivity(LoginActivity.class);
            }
        }
    });

    public BindingCommand moreHouseN = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                startActivity(NoticeHouseActivity.class);
            }else {
                startActivity(LoginActivity.class);
            }
        }
    });



    public BindingCommand follow0 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                Bundle bundle = new Bundle();
                bundle.putInt(AppConfig.BUNDLE_KEY,0);
                startActivity(FollowRecordActivity.class,bundle);
            }else {
                startActivity(LoginActivity.class);
            }
        }
    });

    public BindingCommand follow1 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                Bundle bundle = new Bundle();
                bundle.putInt(AppConfig.BUNDLE_KEY,1);
                startActivity(FollowRecordActivity.class,bundle);
            }else {
                startActivity(LoginActivity.class);
            }
        }
    });


    public ObservableList<TabOneItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<TabOneItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_tab_1_house);
    public ObservableField<Integer> showSz = new ObservableField<>(View.VISIBLE);

    public ObservableList<TabOneItem1VM> observableListht = new ObservableArrayList<>();
    public ItemBinding<TabOneItem1VM> itemBindinght = ItemBinding.of(BR.viewModel, R.layout.item_tab_1_house_1);
    public ObservableField<Integer> showHt = new ObservableField<>(View.GONE);


    public void getHouseMsg(String s){
        model.beleaselist(AppConfig.getCurMap(model))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<TabOneBean>() {
                    @Override
                    public void onNext(TabOneBean o) {
                        if (o.issucc){
                            szNum.set(o.getData().getBe().size()+"");
                            htNum.set(o.getData().getDue().size()+"");
                            if (s.equals("待收租")){
                                observableList.clear();
                                showSz.set(View.VISIBLE);
                                showHt.set(View.GONE);
                                if (o.getData().getBe().size() > 0){
                                    for (TabOneBean.DataDTO.BeDTO bean : o.getData().getBe()) {
                                        if (observableList.size() <= 3){
                                            TabOneItemVM tabOneItemVM = new TabOneItemVM(FOneVM.this);
                                            tabOneItemVM.setData(bean);
                                            observableList.add(tabOneItemVM);
                                        }

                                    }
                                }

                            }else {
                                observableListht.clear();
                                showSz.set(View.GONE);
                                showHt.set(View.VISIBLE);
                                if (o.getData().getDue().size() > 0){
                                    for (TabOneBean.DataDTO.DueDTO bean : o.getData().getDue()) {
                                        if (observableListht.size() <= 3){
                                            TabOneItem1VM tabOneItemVM = new TabOneItem1VM(FOneVM.this);
                                            tabOneItemVM.setData(bean);
                                            observableListht.add(tabOneItemVM);
                                        }

                                    }
                                }
                            }


                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public ObservableField<TabOne2Bean.DataDTO> followEntity = new ObservableField<>();

    public void getFollowData(){
        model.homerecords(AppConfig.getCurMap(model))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<TabOne2Bean>() {
                    @Override
                    public void onNext(TabOne2Bean o) {
                        if (o.issucc){
                            followEntity.set(o.getData());
                            progressView.setValue(100,followEntity.get().getRentalRate()).takeEffect();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getSchedule(){
        model.myschedule(AppConfig.getCurMap(model))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<TabOneScheduleBean>() {
                    @Override
                    public void onNext(TabOneScheduleBean o) {
                        if (o.issucc){
                            setScheduleData(o.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableField<Integer> showNo = new ObservableField<>();

    public ObservableList<TabOneSchduleItemVM> observableListS = new ObservableArrayList<>();
    public ItemBinding<TabOneSchduleItemVM> itemBindingS = ItemBinding.of(BR.viewModel,R.layout.item_schedule);
    private void setScheduleData(List<TabOneScheduleBean.DataDTO> data) {
        observableListS.clear();


        if (ValidateUtils.isNotEmptyObjectOrString(data.get(0))){
            if (data.get(0).getMyList().size() > 0){
                showNo.set(View.GONE);
                for (TabOneScheduleBean.DataDTO.MyListDTO bean : data.get(0).getMyList()) {
                    TabOneSchduleItemVM tabOneSchduleItemVM = new TabOneSchduleItemVM(FOneVM.this);
                    tabOneSchduleItemVM.setData(bean);
                    observableListS.add(tabOneSchduleItemVM);
                }
            }else {
                showNo.set(View.VISIBLE);
            }
        }else {
            showNo.set(View.VISIBLE);
        }


        TabOneScheduleAdapter adapter = new TabOneScheduleAdapter();
        mShcheduleRecy.setAdapter(adapter);
        adapter.setList(data);
        adapter.addChildClickViewIds(R.id.item);


        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            observableListS.clear();
            adapter.setSel(position);
            if (data.get(position).getMyList().size() > 0){
                showNo.set(View.GONE);
                for (TabOneScheduleBean.DataDTO.MyListDTO bean : data.get(position).getMyList()) {
                    TabOneSchduleItemVM tabOneSchduleItemVM = new TabOneSchduleItemVM(FOneVM.this);
                    tabOneSchduleItemVM.setData(bean);
                    observableListS.add(tabOneSchduleItemVM);
                }
            }else {
                showNo.set(View.VISIBLE);
            }
        });

        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getDate().equals(ValidateUtils.getCurDate())){
                adapter.setSel(i);
            }
        }




    }

    RecyclerView mShcheduleRecy;
    public void setScheduleRecy(RecyclerView scheduleRecy) {
        mShcheduleRecy = scheduleRecy;
    }

    SemicircleProgressView progressView;
    public void setPro(SemicircleProgressView pro) {
        progressView = pro;
    }
}
